<%-- 
    Document   : timer
    Created on : 23-Nov-2012, 19:09:12
    Author     : karamelka
--%>

<%@page import="eSales.com.DAL.BidManager"%>
<%@page import="eSales.com.DAL.LotManager"%>
<%@page import="java.util.ArrayList"%>
<%@page import="eSales.com.DAL.Bids"%>
<%@page import="java.util.List"%>
<%@page import="javax.persistence.EntityManager"%>
<%@page import="javax.persistence.Persistence"%>
<%@page import="javax.persistence.EntityManagerFactory"%>
<%@page import="eSales.com.DAL.Lot"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
    LotManager lotManager = new LotManager();
    Lot lot = lotManager.getLot(Integer.parseInt(request.getParameter("id")));

    BidManager bidManager = new BidManager();
    List<Bids> bids = null;
    Date date = null;

    if (lot.getType().equals("auction")) {
        bids = bidManager.getBids(lot.getLotId());
    }else if (lot.getType().equals("tender")) {
        date = lot.getEndTime();
    }

%>
<html>
    <head>
        <link type="text/css" rel="stylesheet" href="style.css">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link href="./resources/css/default.css" rel="stylesheet" type="text/css" />
        <link href="./resources/css/cssLayout.css" rel="stylesheet" type="text/css" />
        <!DOCTYPE html>
    <script type="text/javascript">
        function bla(){   
            ajaxFunction();
            setInterval('ajaxFunction()',1000);
        }
 
        function getXMLObject()  //XML OBJECT
        {
            var xmlHttp = false;
            try {
                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
            }
            catch (e) {
                try {
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
                }
                catch (e2) {
                    xmlHttp = false   // No Browser accepts the XMLHTTP Object then false
                }
            }
            if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
                xmlHttp = new XMLHttpRequest();        //For Mozilla, Opera Browsers
            }
            return xmlHttp;  // Mandatory Statement returning the ajax object created
        }
 
        var xmlhttp = new getXMLObject();	//xmlhttp holds the ajax object
 
        function ajaxFunction() {
            if(xmlhttp) {
                xmlhttp.open("GET","Timer?time=" + <%=date.getTime()%> , true); //gettime will be the servlet name
                xmlhttp.onreadystatechange  = handleServerResponse;
                xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                xmlhttp.send(null);
            }
        }
 
        function handleServerResponse() {
            if (xmlhttp.readyState == 4) {
                if(xmlhttp.status == 200) {
                    var element = document.getElementById("time");
                    element.innerHTML = xmlhttp.responseText;             
                }
                else {
                    alert("Error during AJAX call. Please try again");
                }
            }
        }
    </script>
</head>

<body onload="bla()">
    <div id="navigation">
        Welcome, <u><b> <%=request.getRemoteUser() == null ? "guest" : request.getRemoteUser()%></b></u><a href="logout.jsp">   Logout</a>
        <ul style="float: right; text-align: right;">
            <li><a href="index.jsp">home</a> | </li>
            <li><a href="login.jsp">login</a> | </li>
            <% if (request.getRemoteUser() != null) {%>
            <li><a href="SecuredUser/userPage.jsp" style="color:red">info</a> | </li>
            <li><a href="SecuredAdmin/adminPage.jsp" style="color:red">admin</a></li>	
            <% }%>
        </ul>
    </div>
    <div id="top">
        <img src="./resources/images/icon2.png" alt="eSales" width="84" height="84" align="right" class="limeimage" />
        <h1>eSales / <%=lot.getName()%></h1>
        <p>http://www.eSales.com/</p>
    </div>
    <div id="content">
        <div id="maintextarea">
            <table class="table1">
                <tr>
                    <th></th>
                    <th><%=lot.getName()%></th>
                </tr>
                <tr>
                    <th>Description</th>
                    <td><%=lot.getDescription()%></td>
                </tr>
                <tr>
                    <th>Start Price</th>
                    <td><%=lot.getStartPrice()%>$</td>
                </tr>
                <% if (lot.getType().equals("tender")) {%>
                <tr>
                    <th>Close in:</th>
                    <td id="tr1">
                        <div id="time"></div>
                    </td>
                </tr>
                <% }%>
                <tr>
                    <td></td>
                    <td>
                        <form action="AddBid" method="POST">
                            <input type="hidden" name="lotId" value="<%=lot.getLotId()%>">
                            <input type="text" name="bidSum">
                            <input type="submit" value="Bid">
                        </form>
                    </td>
                </tr>
            </table>

            <br/><br/>

            <% if (lot.getType().equals("auction")) {%>
            <table class="table1">
                <tr>
                    <th>User</th>
                    <th>Bid</th>  
                </tr>
                <%   for (Bids b : bids) {%>
                <tr>
                    <td><%=b.getUserName()%></td>
                    <td><%=b.getBid()%></td>
                </tr>
                <% }%>
            </table>
            <% }%>
        </div>

    </div>
    <div id="bottom">
    </div>
</body>
</html>